//侧边栏 前4项的页面跳转
var li_list = document.querySelectorAll(".select li")
li_list[0].addEventListener('click',function(){
    location.href = "/chapter12"
})
li_list[1].addEventListener('click',function(){
    location.href = "/chapter12/upload"
})
li_list[2].addEventListener('click',function(){
    location.href = "/chapter12/search"
})
li_list[3].addEventListener('click',function(){
    location.href = "/chapter12/mynode"
})

//获取用户名展示欢迎标语
let userCookie = getCookie("username")
if(userCookie){
    document.querySelector(".l_img p").innerHTML= "欢迎!" + " " +userCookie
}

//搜索笔记的点击事件
var selectNode = document.querySelector("input[type='submit']")
selectNode.addEventListener('click',select)
//初始化搜索
select()
function select (){
    //获取搜索条件
    let nodeName = document.querySelector("input[name='nodeName']").value
    let author = document.querySelector("input[name='author']").value
    let popularity = document.querySelector("select[name='popularity']").value
    let year = document.querySelector("select[name='year']").value
    let month = document.querySelector("select[name='month']").value
    let order = document.querySelector("select[name='order']").value
    let nodeClass = document.querySelector("select[name='class']").value
    let language = document.querySelector("select[name='language']").value
    //console.log("搜索条件：" + "笔记名为" + nodeName + "作者为：" + author + "有着" + popularity + "人气，在" + year + "年" + month + "月发布的"  + nodeClass + "类" + language + "语言" + order)
    //拼接年和月为字符串，提供给后端Node类的time属性
    year = year =="none" ? "0000" : year
    month = month =="none" ? "00" : month
    const dateTime = year + "-" + month + "-" + "00"
    //判断人气是否有填，没填默认为0
    popularity = popularity=="none"? 0 : popularity
    //发送Ajax
    $.ajax({
        url:"node/select",
        type:"post",
        data:JSON.stringify({
            nodename:nodeName,
            username:author,
            popularity:popularity,
            time:dateTime,
            class_name:nodeClass,
            language:language
        }),
        contentType:"application/json;charset=UTF-8",
        dataType:"json",
        success:function (data){
            if(order=="popularity"){ //若排序选的是人气最高排序
                for (var i=0;i<data.length;i++){ //冒泡排序
                    for(var j=0;j<data.length;j++){
                        if(data[j].popularity < data[i].popularity) {
                            console.log("交换："+ data[j] + data[i] + "长度" + data.length)
                            var temp = data[j]
                            data[j] = data[i]
                            data[i] =temp
                        }
                    }
                }
            }
            if(order=="later"){ //若排序选的是时间最近排序
                for (var i=0;i<data.length;i++){ //冒泡排序
                    for(var j=0;j<data.length;j++){
                        if(data[j].time < data[i].time) {
                            console.log("交换："+ data[j] + data[i] + "长度" + data.length)
                            var temp = data[j]
                            data[j] = data[i]
                            data[i] =temp
                        }
                    }
                }
            }
            //获取list
            const lu = document.querySelector("#list")
            let struct = ""
            for (var i=0;i<data.length;i++){
                struct += ' <li>\n' +
                    '                    <h2>' + data[i].nodename + '</h2>\n' +
                    '                    <div id="class">' + data[i].class_name +'</div>\n' +
                    '                    <div id="language">' + data[i].language + '</div>\n' +
                    '                    <div id="author">' + data[i].username + '</div>\n' +
                    '                    <div id="day">' + data[i].time + '</div>\n' +
                    '                    <div id="popularity"><span></span>' + data[i].popularity + '</div>\n' +
                    '                    <p id="introduction">这里是对笔记的一些简介，介绍笔记的内容</p>\n' +
                    '                </li>'
            }
            lu.innerHTML = struct
            // 为每个标签添加点击下载事件
            li_addListener()
        }
    })
}
//初始化添加点击下载事件
li_addListener()
//添加点击笔记下载事件
function li_addListener(){
    let li_all = document.querySelectorAll("#list li") //获取全部li
    //遍历添加点击事件
    for(let i=0;i<li_all.length;i++){
        li_all[i].addEventListener('click',function (e){
            let children = this.children
            //console.log(children)
            let node_name = children[0].innerText
            let class_name = children[1].innerText
            let language = children[2].innerText
            let author = children[3].innerText
            //不能使用ajax发送请求找笔记下载，userId-类型-语言-笔记名.md，因为ajax不支持流的形式返回
            window.location.href = basePath() + "node/downloadTry?nodename=" + node_name + "&class_name=" + class_name + "&language=" + language + "&username=" + author
        })
    }
}

//获取服务器地址
function basePath() {
    //获取当前网址，如： http://localhost:8080/ems/Pages/Basic/Person.jsp
    var curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： /ems/Pages/Basic/Person.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8080
    var localhostPath = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/ems
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    //获取项目的basePath   http://localhost:8080/ems/
    var basePath = localhostPath + projectName + "/";
    return basePath;
};